package org.ithasu.uBroadcast.test;

import java.net.SocketTimeoutException;

import org.ithasu.uBroadcast.UMessage;
import org.ithasu.uBroadcast.connectors.UDPConnector;

public class LostPacketRecv {

	public static void main(String[] args) {
		try {
			UDPConnector conn = new UDPConnector("lostrx".getBytes(),
					UDPConnector.DEFAULT_PORT, 0);

			int nb_received = 0;
			int nb_lost = 0;
			int nb_error = 0;
			int nb_timeout = 0;
			int last_received = 0;
			while (true) {
				try {
					UMessage msg = conn.recv();
					if (msg != null) {
						nb_received++;
						int just_received = (int) msg.getType();
						if (just_received < last_received)
							just_received += 256;
						nb_lost += (just_received - last_received) - 1;
						last_received = just_received;
					} else {
						nb_error++;
					}
				} catch (SocketTimeoutException e) {
					// Pass
					nb_timeout++;
				}
				System.out.println(nb_received + " received, " + nb_lost
						+ " lost, " + nb_error + " errors, " + nb_timeout
						+ " timed out.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
